﻿using Yunwei.IBizLogic;
using Yunwei.IDAL;
using System.Linq;
using System;

namespace Yunwei.BizLogic
{
    /// <summary>
    /// 服务基类
    /// <remarks>创建：2014.02.03
    /// 修改：2014.03.04</remarks>
    /// </summary>
    public abstract class BaseService<T> : IBaseService<T> where T : class
    {
        protected IBaseRepository<T> CurrentRepository { get; set; }

        public BaseService(IBaseRepository<T> currentRepository) { CurrentRepository = currentRepository; }

        public T Add(T entity) { return CurrentRepository.Add(entity); }

        public bool Update(T entity) { return CurrentRepository.Update(entity); }

        public bool Delete(T entity) { return CurrentRepository.Delete(entity); }

        public T Find(int ID) { return CurrentRepository.Find(ID); }

        public IQueryable<T> PageList(IQueryable<T> entitys, int pageIndex, int pageSize)
        {
            return entitys.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }

        public int Delete(T[] entitys)
        {
            foreach(T entity in entitys)
            {
                CurrentRepository.Delete(entity, false);
            }

            return CurrentRepository.Save();
        }
    }
}
